package com.example.qxfw.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

@Slf4j
public class FileUtil {

    /**
     * 读取文件内容
     * @param file
     * @return
     * @throws IOException
     */
    public static String readLine(File file) throws IOException {
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
        BufferedReader bufferedReader = new BufferedReader(read);

        StringBuilder lineTxt = new StringBuilder();
        String text;
        while ((text = bufferedReader.readLine()) != null) {
            lineTxt.append(text);
        }
        return lineTxt.toString();
    }

    /**
     * 获取文件路径
     * @param suffix
     * @return
     */
    public static File getFile(String suffix){
        String fileName = UUID.randomUUID()+"."+suffix;
        File file = new File(getFilePath(),fileName);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()){
            boolean mkdir = parentFile.mkdir();
            log.info(mkdir?"文件夹创建成功":"文件夹创建失败");
        }
        return file;
    }

    /**
     * 获取文件夹路径
     * @return
     */
    private static String getFilePath(){
        ApplicationHome applicationHome = new ApplicationHome(FileUtil.class);
        return applicationHome.getDir().getParentFile()
                .getParentFile().getAbsolutePath() + "\\src\\main\\resources\\file";
    }

    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //将Byte数组转换成文件
    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static InputStream getNetFile(String filepath) throws Exception {
        URL url = new URL(filepath);
        //打开连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置请求方式为"GET"
        conn.setRequestMethod("GET");
        //超时响应时间为10秒
        conn.setConnectTimeout(10 * 1000);
        //通过输入流获取图片数据
        InputStream inStream = conn.getInputStream();
        return inStream;
    }
}
